package com.outsourcing.bottle.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.outsourcing.bottle.domain.BottleNetTypeEntry;

/**
 * 捞网类型表
 * @author 06peng
 *
 */
public class BottleNetTypeTable {

	private final String TABLE_BOTTLE_NET_TYPE = "bottle_net_type";
	private DBManagerImpl db = null;
	public BottleNetTypeTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_NET_TYPE)) {
			createBottleNetType();
		}
	}
	
	private void createBottleNetType() {
		String createSql = "create table if not exists "+TABLE_BOTTLE_NET_TYPE+" (id integer primary key autoincrement, nettypeid integer, nettype_name_cn varchar, nettype_name_en varchar, nettype_desc_cn varchar," +
				"nettype_desc_en varchar, nettype_sjwicon varchar, nettype_isshow integer, nettype_location_select integer)";
		db.creatTable(db.getConnection(), createSql, TABLE_BOTTLE_NET_TYPE);
	}
	
	public ArrayList<BottleNetTypeEntry> getBottleNetTypes() {
		Cursor cursor = null;
		ArrayList<BottleNetTypeEntry> bottleNetTypes = null;
		try {
			cursor = db.find(db.getConnection(), "select * from " + TABLE_BOTTLE_NET_TYPE + " where nettype_isshow = 1", null);
			if (cursor != null) {
				bottleNetTypes = new ArrayList<BottleNetTypeEntry>();
				while (cursor.moveToNext()) {
					BottleNetTypeEntry bottleNetType = new BottleNetTypeEntry();
					bottleNetType.setNettypeid(cursor.getInt(cursor.getColumnIndex("nettypeid")));
					bottleNetType.setNettype_name_cn(cursor.getString(cursor.getColumnIndex("nettype_name_cn")));
					bottleNetType.setNettype_name_en(cursor.getString(cursor.getColumnIndex("nettype_name_en")));
					bottleNetType.setNettype_desc_cn(cursor.getString(cursor.getColumnIndex("nettype_desc_cn")));
					bottleNetType.setNettype_desc_en(cursor.getString(cursor.getColumnIndex("nettype_desc_en")));
					bottleNetType.setNettype_sjwicon(cursor.getString(cursor.getColumnIndex("nettype_sjwicon")));
					bottleNetType.setNettype_isshow(cursor.getInt(cursor.getColumnIndex("nettype_isshow")));
					bottleNetType.setNettype_location_select(cursor.getInt(cursor.getColumnIndex("nettype_location_select")));
					bottleNetTypes.add(bottleNetType);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return bottleNetTypes;
	}
	
	public void saveBottleNetType(BottleNetTypeEntry bottleNetType) {
		ContentValues values = new ContentValues();
		values.put("nettypeid", bottleNetType.getNettypeid());
		values.put("nettype_name_cn", bottleNetType.getNettype_name_cn());
		values.put("nettype_name_en", bottleNetType.getNettype_name_en());
		values.put("nettype_desc_cn", bottleNetType.getNettype_desc_cn());
		values.put("nettype_desc_en", bottleNetType.getNettype_desc_en());
		values.put("nettype_sjwicon", bottleNetType.getNettype_sjwicon());
		values.put("nettype_isshow", bottleNetType.getNettype_isshow());
		values.put("nettype_location_select", bottleNetType.getNettype_location_select());
		db.save(db.getConnection(), TABLE_BOTTLE_NET_TYPE, values);
	}
	
	public BottleNetTypeEntry getBottleNetType(int bottleId) {
		Cursor cursor = null;
		BottleNetTypeEntry bottleNetType = null;
		try {
			cursor = db.find(db.getConnection(), "select * from "
					+ TABLE_BOTTLE_NET_TYPE + " where nettypeid=?",
					new String[] { String.valueOf(bottleId) });
			if (cursor != null) {
				while (cursor.moveToNext()) {
					bottleNetType = new BottleNetTypeEntry();
					bottleNetType.setNettypeid(cursor.getInt(cursor.getColumnIndex("nettypeid")));
					bottleNetType.setNettype_name_cn(cursor.getString(cursor.getColumnIndex("nettype_name_cn")));
					bottleNetType.setNettype_name_en(cursor.getString(cursor.getColumnIndex("nettype_name_en")));
					bottleNetType.setNettype_desc_cn(cursor.getString(cursor.getColumnIndex("nettype_desc_cn")));
					bottleNetType.setNettype_desc_en(cursor.getString(cursor.getColumnIndex("nettype_desc_en")));
					bottleNetType.setNettype_sjwicon(cursor.getString(cursor.getColumnIndex("nettype_sjwicon")));
					bottleNetType.setNettype_isshow(cursor.getInt(cursor.getColumnIndex("nettype_isshow")));
					bottleNetType.setNettype_location_select(cursor.getInt(cursor.getColumnIndex("nettype_location_select")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return bottleNetType;
	}
	
	public void clearTalbe() {
		db.delete(db.getConnection(), TABLE_BOTTLE_NET_TYPE, null, null);
	}
}
